<?
namespace Api
{
    class Image
    {
        const TEMP  = '/tmp/img';
        const THUMB  = '/public/gallery/thumb';
        const PIC = '/public/gallery';
        const INTRO = '/public/intro';
        
        /**
         * Render uploaded image using imagick
         * 
         * @static
         * @param string $image image file name 
         * @return image data
         */
        static public function getUploadedImage($image)
        {
            $im = new \Imagick(BASE_PATH.'/..'.self::TEMP.'/'.$image);
            $im->setImageFormat('jpeg');
            header("Content-Type: image/jpeg");
            echo $im;
        }
        
        static public function getThumbImage($image)
        {
            $im = new \Imagick(BASE_PATH.'/..'.self::THUMB.'/'.$image);
            $im->setImageFormat('jpeg');
            header("Content-Type: image/jpeg");
            echo $im;
        }                
        
         static public function getImage($image)
        {            
            $im = new \Imagick(BASE_PATH.'/..'.self::PIC.'/'.$image);                        
        
            $im->setImageFormat('jpeg');
            header("Content-Type: image/jpeg");
            echo $im;
        }   
         static public function getIntroImage($image)
        {            
            $im = new \Imagick(BASE_PATH.'/..'.self::INTRO.'/'.$image);                        
        
            $im->setImageFormat('jpeg');
            header("Content-Type: image/jpeg");
            echo $im;
        }   
        /**
         * Upload image to tmp folder, return filename.         
         *
         * @param string input_name 
         * @static
         * @return encode filename in md5
         */
        static public function UploadToTemp($input_name)
        {
            $result = array(
                "result"=>false,
                "message"=>"Nie wykryto pola z danymi pliku"
                );
            
            if(array_key_exists($input_name, $_FILES))
            {                
                $file = $_FILES[$input_name];                
                $filename =md5(date('Y-m-d').time());
                if($file['type'] != 'image/jpeg')
                {
                    $result['message'] = "Format pliku nie jest obsługiwany przez system";
                }else{
                    if(move_uploaded_file($file['tmp_name'], BASE_PATH.self::TEMP.'/'.$filename) !== false)
                    {                                
                        $result['result']=true;
                        $result['filename']=$filename;
                        unset($result['message']);
                    }else
                    {
                        $result['message']="Wystąpił błąd przy wysyłaniu pliku";
                    }
                 }
            }
            
            return $result;
        }
        
        /**
         * Move from temp to destination directory with image resizing
         * when $size is array.
         *
         * @param string $image - image file name
         * @param string $dst - destination folder
         * @param array $size - array with dest size
         */
         static public function MoveWithResize($image, $dst, array $size = array())
         {
            if(strlen($image) == 0 && !is_file(BASE_PATH.self::TEMP.'/'.$image)) return false;
            
            $im = new \imagick(BASE_PATH.self::TEMP.'/'.$image);
            
            if(!empty($size))
            {
                $im->scaleImage($size[0], $size[1]);
            }
            
            $im->writeImage(BASE_PATH.$dst.'/'.$image);        
            unlink(BASE_PATH.self::TEMP.'/'.$image);
            
            return true;
         }
    }
}
?>